//
//  AY_BookUpdatePage.m
//  AiYue
//
//  Created by boWen on 2023/10/24.
//

#import "AY_BookUpdatePage.h"
#import "AY_BookUpdateCell.h"
#import "STPhotoKitController.h"
#import "UIImagePickerController+ST.h"
#import "STConfig.h"
#import "AY_BookNickNamePage.h"
#import "AYHttpClient.h"

@interface AY_BookUpdatePage ()<UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UIActionSheetDelegate, STPhotoKitDelegate>
@property (nonatomic, strong) UITableView * baseTableView;
@end

@implementation AY_BookUpdatePage

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateUserInfoModel];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithNav];
}


- (void)initWithNav {
    self.gk_navTitle = @"个人信息";
    [self.view addSubview:self.baseTableView];
    [self updateUserInfoModel];
}


- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT - GK_STATUSBAR_NAVBAR_HEIGHT)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.scrollEnabled = NO;
        [myTableView registerClass:[AY_BookUpdateCell class] forCellReuseIdentifier:@"AY_BookUpdateCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookUpdateCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookUpdateCell"];
    [cell setData:indexPath userInfo:self.userInfoModel];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(65);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return YNUM(16);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {
        [self editImageSelected];
    }else if(indexPath.row == 1) {
        AY_BookNickNamePage * page = [AY_BookNickNamePage new];
        page.nickName = self.userInfoModel.nickName;
        [self.navigationController pushViewController:page animated:YES];
    }else if(indexPath.row == 3) {
        [self editSexSelected];
    }
}









#pragma mark - 1.STPhotoKitDelegate的委托

- (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage {
 
    NSData *data = UIImageJPEGRepresentation(resultImage, 1.0f);
    [AYApiRequest postCommonUploadType:@"img" andFileParam:@{@"fileName":[NSString stringWithFormat:@"%@-coverImg.png",self.userInfoModel.userName],@"fileType":@"png"} andFileData:data success:^(AY_UploadModel * _Nonnull pkg) {
        [AYApiRequest postUserInfoUpdate:pkg.data.ossUrl param:@"avatar" updateType:@"4" success:^(AY_UserUpdateModel * _Nonnull pkg) {
            if([pkg.code isEqualToString:@"200"]) {
                [self updateUserInfoModel];
                ShowToast(@"头像上传成功");
            }
        } failure:^(int status, NSString * _Nonnull info) {
            ShowToast(@"亲，网络不顺畅哟~");
        }];
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}



#pragma mark - 2.UIImagePickerController的委托

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
        STPhotoKitController *photoVC = [STPhotoKitController new];
        [photoVC setDelegate:self];
        [photoVC setImageOriginal:imageOriginal];
        [photoVC setSizeClip:CGSizeMake(ScreenWidth * 0.8, ScreenHeight * 0.5)];
        photoVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:photoVC animated:YES completion:nil];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^(){
    }];
}

#pragma mark - --- event response 事件相应 ---
- (void)editImageSelected
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"选择您要提交的照片" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];

        if ([controller isAvailableCamera] && [controller isSupportTakingPhotos]) {
            [controller setDelegate:self];
            [self presentViewController:controller animated:YES completion:nil];
        }else {
            NSLog(@"%s %@", __FUNCTION__, @"相机权限受限");
        }
    }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [controller setDelegate:self];
        if ([controller isAvailablePhotoLibrary]) {
            [self presentViewController:controller animated:YES completion:nil];
        }    }];

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }];

    [alertController addAction:action0];
    [alertController addAction:action1];
    [alertController addAction:action2];

    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)editSexSelected
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"请选择性别" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [AYApiRequest postUserInfoUpdate:@"1" param:@"gender" updateType:@"2" success:^(AY_UserUpdateModel * _Nonnull pkg) {
            if([pkg.code isEqualToString:@"200"]) {
                [self updateUserInfoModel];
            }
        } failure:^(int status, NSString * _Nonnull info) {
            ShowToast(@"亲，网络不顺畅哟~");
        }];

    }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [AYApiRequest postUserInfoUpdate:@"2" param:@"gender" updateType:@"2" success:^(AY_UserUpdateModel * _Nonnull pkg) {
            if([pkg.code isEqualToString:@"200"]) {
                [self updateUserInfoModel];
            }
        } failure:^(int status, NSString * _Nonnull info) {
            ShowToast(@"亲，网络不顺畅哟~");
        }];

    }];

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }];

    [alertController addAction:action0];
    [alertController addAction:action1];
    [alertController addAction:action2];

    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)updateUserInfoModel {
    [AYApiRequest getUserInfoSuccess:^(AY_UserModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            self.userInfoModel = pkg.data;
            [self.baseTableView reloadData];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

@end
